import Layout from '@/layout'
import PageContent from '@/layout/pageContent.vue'
import {LOGICAL, MENU_TYPE} from "@/constantData";
import {isArr} from "@/utils/arrayUtil";
import {authentication} from "@/jurisdiction/authentication";

let businessRouter = [
  {
    path: '',
    redirect: '/securityCheck/ztDefectInspectList',
    component: Layout,
    children: [
      // 安全检查
      // {
      //   path: '/securityCheck',
      //   component: PageContent,
      //   redirect: '/securityCheck/ztDefectInspect',
      //   name: 'securityCheck',
      //   authority: {role: ['A:B:A'], logical: LOGICAL.OR},
      //   hidden: false,
      //   menuType: MENU_TYPE.CATALOG,
      //   parentPath: '',
      //   meta: {title: '安全检查'},
      //   children: [
      //     // 专题检查
      //     {
      //       path: 'ztDefectInspectList',
      //       keep: false,
      //       authority: {role: ['A:B:A'], logical: LOGICAL.OR},
      //       hidden: false,
      //       parentPath: '/securityCheck',
      //       menuType: MENU_TYPE.MENU,
      //       component: () => import('@/views/securityCheck/ztDefectInspect/listPage.vue'),
      //       name: 'ztDefectInspect_list',
      //       meta: {title: '专题检查'}
      //     },
      //     // 专题检查--新增/修改
      //     {
      //       path: 'ztDefectInspectEdit',
      //       keep: false,
      //       authority: {role: ['A:B:A'], logical: LOGICAL.OR},
      //       hidden: true,
      //       parentPath: '/securityCheck/ztDefectInspectList',
      //       menuType: MENU_TYPE.INFO,
      //       component: () => import('@/views/securityCheck/ztDefectInspect/editPage.vue'),
      //       name: 'ztDefectInspect_edit',
      //       meta: {title: '专题检查编辑'},
      //       children: []
      //     },
      //     // 专题检查--详情
      //     {
      //       path: 'ztDefectInspectInfo',
      //       keep: false,
      //       authority: {role: ['A:B:A'], logical: LOGICAL.OR},
      //       hidden: true,
      //       parentPath: '/securityCheck/ztDefectInspectList',
      //       menuType: MENU_TYPE.INFO,
      //       component: () => import('@/views/securityCheck/ztDefectInspect/infoPage.vue'),
      //       name: 'ztDefectInspect_info',
      //       meta: {title: '专题检查详情'},
      //       children: []
      //     },
      //
      //     // 隐患问题管理
      //     {
      //       path: 'defectQuestion',
      //       keep: false,  //为false时不缓存页面
      //       authority: {role: ['A:B:A'], logical: LOGICAL.OR},
      //       hidden: true,
      //       parentPath: '/securityCheck/ztDefectInspectList',
      //       menuType: MENU_TYPE.INFO,
      //       component: () => import('@/views/securityCheck/defectQuestion/listPage'),
      //       name: 'defectQuestion_list',
      //       meta: {title: '隐患问题管理'}
      //     },
      //     // 隐患问题管理--新增/修改
      //     {
      //       path: 'defectQuestionEdit',
      //       keep: false,
      //       authority: {role: ['A:B:A'], logical: LOGICAL.OR},
      //       hidden: true,
      //       parentPath: '/securityCheck/ztDefectInspectList/defectQuestion',
      //       menuType: MENU_TYPE.INFO,
      //       component: () => import('@/views/securityCheck/defectQuestion/editPage'),
      //       name: 'defectQuestion_edit',
      //       meta: {title: '隐患问题编辑'},
      //       children: []
      //     },
      //     // 隐患问题管理--详情
      //     {
      //       path: 'defectQuestionInfo',
      //       keep: false,
      //       authority: {role: ['A:B:A'], logical: LOGICAL.OR},
      //       hidden: true,
      //       parentPath: '/securityCheck/ztDefectInspectList/defectQuestion',
      //       menuType: MENU_TYPE.INFO,
      //       component: () => import('@/views/securityCheck/defectQuestion/infoPage'),
      //       name: 'defectQuestion_info',
      //       meta: {title: '隐患问题详情'},
      //       children: []
      //     },
      //   ],
      // },

      // 安全检查
      {
        path: '/securityCheck',
        component: PageContent,
        redirect: '/securityCheck/ztDefectInspect',
        name: 'securityCheck',
        authority: {role: ['business:menu:aqjc'], logical: LOGICAL.OR},
        hidden: false,
        menuType: MENU_TYPE.CATALOG,
        parentPath: '',
        meta: {title: '安全检查'},
        children: [
          // 专题检查
          {
            path: 'ztDefectInspectList',
            keep: false,
            authority: {role: ['business:menu:ztjc'], logical: LOGICAL.OR},
            hidden: false,
            parentPath: '/securityCheck',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/checkPage/index.vue'),
            name: 'checkPage',
            meta: {title: '专题检查'}
          },
          // 隐患统计
          {
            path: 'statistics',
            keep: false,
            authority: {role: ['business:menu:ztjc'], logical: LOGICAL.OR},
            hidden: true,
            parentPath: '/securityCheck/ztDefectInspectList',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/checkStatisticsPage/index.vue'),
            name: 'check_statistics_page',
            meta: {title: '隐患统计'}
          },
          // 专题检查子业务
          {
            path: 'checkBusPage',
            keep: false,
            authority: {role: ['business:menu:ztjc'], logical: LOGICAL.OR},
            hidden: true,
            parentPath: '/securityCheck/ztDefectInspectList',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/checkBusPage/index.vue'),
            name: 'check_sub_page',
            meta: {title: '专题检查子业务'}
          },
          // 专题检查子业务详情
          {
            path: 'checkBusPageInfo',
            keep: false,
            authority: {role: ['business:menu:ztjc'], logical: LOGICAL.OR},
            hidden: true,
            parentPath: '/securityCheck/ztDefectInspectList',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/checkBusPage/subInfoPage/index.vue'),
            name: 'check_sub_page_info',
            meta: {title: '专题检查子业务详情'}
          },
        ],
      },

      // 业务配置
      {
        path: '/business',
        component: PageContent,
        redirect: '/business/defectTypeList',
        name: 'defectType',
        authority: {role: ['A:B:A'], logical: LOGICAL.OR},
        hidden: false,
        menuType: MENU_TYPE.CATALOG,
        parentPath: '',
        meta: {title: '业务配置'},
        children: [
          // 隐患类别管理
          {
            path: 'defectTypeList',
            keep: true,
            authority: {role: ['A:B:A'], logical: LOGICAL.OR},
            hidden: false,
            parentPath: '/business',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/system/defectType/listPage.vue'),
            name: 'defectType_list',
            meta: {title: '隐患类别管理'}
          },
          // 隐患类别子类
          {
            path: 'defectTypeInfo',
            keep: false,
            authority: {role: ['A:B:A'], logical: LOGICAL.OR},
            hidden: false,
            parentPath: '/business/defectTypeList',
            menuType: MENU_TYPE.INFO,
            component: () => import('@/views/system/defectType/infoPage.vue'),
            name: 'defectType_info',
            meta: {title: '隐患类别详情'},
            children: []
          },
        ],
      },

      // 系统管理
      {
        path: '/system',
        component: PageContent,
        redirect: '/system/sysSupervisionDeptList',
        name: 'defectType',
        authority: {role: ['business:menu:xtgl'], logical: LOGICAL.OR},
        hidden: false,
        menuType: MENU_TYPE.CATALOG,
        parentPath: '',
        meta: {title: '系统管理'},
        children: [
          // 监管部门管理
          {
            path: 'sysSupervisionDeptList',
            keep: true,
            authority: {role: ['business:menu:jgbm'], logical: LOGICAL.OR},
            hidden: false,
            parentPath: '/system',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/system/sysSupervisionDept/listPage.vue'),
            name: 'sysSupervisionDept_list',
            meta: {title: '监管部门管理'}
          },

          // 企业管理
          {
            path: 'sysEnterpriseList',
            keep: true,
            authority: {role: ['business:menu:qygl'], logical: LOGICAL.OR},
            hidden: false,
            parentPath: '/system',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/system/sysEnterprise/listPage.vue'),
            name: 'sysEnterprise_list',
            meta: {title: '企业管理'}
          },

          // 用户管理
          {
            path: 'sysUserList',
            keep: true,
            authority: {role: ['business:menu:yhgl'], logical: LOGICAL.OR},
            hidden: false,
            parentPath: '/system',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/system/sysUser/listPage.vue'),
            name: 'sysUser_list',
            meta: {title: '用户管理'}
          },

          // 字典管理
          {
            path: 'dictList',
            keep: true,
            authority: {role: ['A:B:B'], logical: LOGICAL.OR},
            hidden: false,
            parentPath: '/system',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/system/dict/index.vue'),
            name: 'dict_list',
            meta: {title: '字典管理'}
          },
          // 字典类别子类
          {
            path: 'dictInfo',
            keep: false,
            authority: {role: ['A:B:B'], logical: LOGICAL.OR},
            hidden: false,
            parentPath: '/system/dictList',
            menuType: MENU_TYPE.INFO,
            component: () => import('@/views/system/dict/data.vue'),
            name: 'dict_info',
            meta: {title: '字典数据'},
            children: []
          },
        ],
      },

      // 个人中心
      {
        path: '/mine',
        component: PageContent,
        redirect: '/mine/personalSetting',
        name: 'mine',
        hidden: true,
        menuType: MENU_TYPE.CATALOG,
        parentPath: '',
        meta: {title: '个人中心'},
        children: [
          // 个人设置
          {
            path: 'personalSetting',
            keep: false,
            authority: {role: ['A:B:C'], logical: LOGICAL.OR},
            hidden: false,
            parentPath: '/mine',
            menuType: MENU_TYPE.MENU,
            component: () => import('@/views/mine'),
            name: 'personal_setting',
            meta: {title: '个人设置'}
          },
        ],
      },
    ]
  },
]

const changeBusinessRouter = () => {
  const root = _.cloneDeep(businessRouter[0]);

  // 移除不需要的节点
  root.children = removeInfoNodes(_.cloneDeep(root.children));

  // 进行深度优先搜索
  const stack = [root];
  while (stack.length > 0) {
    const node = stack.pop();

    node.hidden = node.hidden ? true : (node.authority != null ? !authentication(node.authority) : false)

    if (isArr(node.children)) {
      stack.push(...node.children);
    }
  }

  return [root];
};

const getKeepList = () => {
  return getPropertyArray(_.cloneDeep(businessRouter[0]))
}

const getPropertyArray = (node) => {

  let result = [];

  // 检查节点的属性是否存在
  if (node.keep && node.name != null && node.name != '') {
    result.push(node.name);
  }

  // 遍历子节点
  if (isArr(node.children)) {
    for (let i = 0; i < node.children.length; i++) {
      const child = node.children[i];
      result = result.concat(getPropertyArray(child));
    }
  }

  return result;
};

const removeInfoNodes = (routes) => {
  const newRoutes = [];

  routes.forEach(route => {
    if (route.menuType !== MENU_TYPE.INFO) {
      // 如果当前节点的 menuType 不是 MENU_TYPE.INFO，则将其添加到新路由配置中
      const newRoute = {...route};

      if (route.children) {
        // 递归处理子节点
        newRoute.children = removeInfoNodes(route.children);
      }

      newRoutes.push(newRoute);
    }
  });

  return newRoutes;
}

const getNavigationBaseList = () => {
  return traverseTree(businessRouter, '')
}

const traverseTree = (tree, parentPath = '') => {
  let result = [];

  for (const node of tree) {
    const path = `${parentPath}/${node.path}`.replace(/\/+/g, '/');

    result.push({title: node?.meta?.title, parentPath: node.parentPath, path, menuType: node.menuType});

    if (node.children && node.children.length > 0) {
      const childNodes = traverseTree(node.children, path);
      result = [...result, ...childNodes];
    }
  }

  return result;
};

export {businessRouter, getNavigationBaseList, getKeepList, changeBusinessRouter}
